package com.sgb.kjwl.view.ui.adapter.transport;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.RunningCityEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class MainCitiesAdapter extends BaseRecycleAdapter<RunningCityEntity> {

    public ItemChildCLickListener itemChildCLickListener;
    ImageView delete;
    LinearLayout select;
    TextView no, cityName;

    public void setItemChildCLickListener(ItemChildCLickListener itemChildCLickListener) {
        this.itemChildCLickListener = itemChildCLickListener;
    }

    public MainCitiesAdapter(Context context, List<RunningCityEntity> datas) {
        super(context, R.layout.item_main_cities, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, RunningCityEntity data) {
        holder.setBinding(BR.viewModel, data);
        no = holder.getItem().findViewById(R.id.no);
        cityName = holder.getItem().findViewById(R.id.city_name);
        no.setText(String.valueOf(holder.getAdapterPosition()+1));
        cityName.setText((data.getCityName().length() > 0 ? data.getCityName() : "请选择城市"));
        cityName.setTextColor(data.getCityName().length() > 0 ? Color.parseColor("#333333") : Color.parseColor("#C0C4CC"));



    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        delete = holder.getItem().findViewById(R.id.delete);

        select = holder.getItem().findViewById(R.id.select);

        delete.setVisibility(View.VISIBLE);
        select.setOnClickListener(view -> {
            if (itemChildCLickListener != null) {
                itemChildCLickListener.selectCity(select, position);
            }
        });
        delete.setOnClickListener(view -> {
            if (itemChildCLickListener != null) {
                itemChildCLickListener.deleteOne(position);
            }
        });
    }

    public interface ItemChildCLickListener {
        void deleteOne(int position);

        void selectCity(LinearLayout root, int position);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
}
